package com.bl.student.dao.redis.client;

import lombok.extern.log4j.Log4j2;
import org.aeonbits.owner.ConfigCache;
import org.redisson.Redisson;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.springframework.stereotype.Service;

/**
 * Created by yujingyi on 2017/5/11.
 */
@Log4j2
@Service
public class RedisService {
    //    private static RedissonClient redisson;
    private static RAtomicLong rAtomicLong;
    private static final String PROJECT_ID_KEY = "sicgi:project:id";
    private static final long initProjectId = 6000000L;

//    @PostConstruct
//    public void init() {
//        RedisConfig redisConfig = ConfigCache.getOrCreate(RedisConfig.class);
//        Config config = new Config();
//        config.setCodec(StringCodec.INSTANCE);
//        config.useSingleServer()
//                .setAddress(redisConfig.host())
//                .setDatabase(redisConfig.db())
//                .setPassword(redisConfig.auth())
//                .setConnectionPoolSize(64)
//                .setConnectionMinimumIdleSize(10);
//        redisson = Redisson.create(config);
//    }

    /**
     * 获取redisson实例
     *
     * @return
     */
    public RedissonClient getInstance() {
        log.info("initialize redisson client...");
        System.out.println("initialize redisson client...");
        RedisConfig redisConfig = ConfigCache.getOrCreate(RedisConfig.class);
        Config config = new Config();
        config.setCodec(StringCodec.INSTANCE);
        config.useSingleServer()
                .setAddress(redisConfig.host())
                .setDatabase(redisConfig.db())
                .setPassword(redisConfig.auth())
                .setConnectionPoolSize(128)
                .setConnectionMinimumIdleSize(10);
//        config.useSentinelServers()
//                .setMasterName(redisConfig.masterName())
//                .addSentinelAddress(redisConfig.sentinel1())
//                .addSentinelAddress(redisConfig.sentinel2())
//                .setPassword(redisConfig.sentinelAuth())
//                .setMasterConnectionPoolSize(64)
//                .setSlaveConnectionPoolSize(64)
//                .setMasterConnectionMinimumIdleSize(10)
//                .setSlaveConnectionMinimumIdleSize(10);
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }


    public static void main(String[] args) {
        RedisService redisService = new RedisService();
//        redisService.init();
    }
}
